package com.itcast.exception;

import com.itcast.pojo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
public class GlobalEeceptionHandler {
    @ExceptionHandler(Exception.class)
    public Result exceptionHandler(Exception e) {
        log.error("服务器异常：{}", e.getMessage()+e.getClass());
        return Result.error("服务器异常");
    }

    @ExceptionHandler(DataIntegrityViolationException.class)
    public Result handleDataIntegrityViolationException(DataIntegrityViolationException e) {
        log.error("数据完整性异常：{}", e.getMessage());
        if (e.getMessage().contains("Duplicate entry")) {
            return Result.error("手机号已存在");
        }
        return Result.error("数据完整性异常");
    }
}
